<?php

namespace app\admin\controller;

use app\models\Admin;
use app\models\{User as Users, MachineLog, Machine, Hall, HallTrade, Wallet, Config, Price};
use think\Db;
use think\Facade\Config as Configs;

class Main extends Base
{

    public function welcome()
    {

        $data = [];
        $data['price'] = (new Price())->where('date', date('Y-m-d'))->value('price') ?? 0; //会员总数
        $exchange = (new Config())->where('key_value', 'exchange')->value('value') ?? 0;
        $hallconfig = (new Config())->where('key_value', 'hallconfig')->value('value');
        $hallconfig = json_decode($hallconfig, true) ?? [];
        $usdt = (new Config())->where('key_value', 'usdt')->value('value') ?? 0;
        $minprice = $hallconfig['minprice'] ?? 0;
        $maxprice = $hallconfig['maxprice'] ?? 0;
        $data['minprice'] = floor($data['price'] * (1 - $minprice / 100) * $usdt * 100) / 100;
        $data['maxprice'] = floor($data['price'] * (1 + $maxprice / 100) * $usdt * 100) / 100;
        $data['exchange'] = $exchange;
        $data['user'] = (new Users())->count(); //会员总数
        $data['authuser'] = (new Users())->where('auth', 2)->count(); //认证会员
        $data['hall'] = (new Hall())->where(['status' => [0, 1]])->sum('surplus'); //进行中求购量
        $data['halltrade'] = (new HallTrade())->where(['status' => [0, 1, 8, 9]])->sum('num'); //进行中交易量
        $data['halltradeappeal'] = (new HallTrade())->where(['status' => [8, 9]])->count(); //进行中交易量
        $data['halltradeend'] = (new HallTrade())->where(['status' => 2])->sum('num'); //累计已成交
        $data['wallet'] = (new Wallet())->sum('money'); //待提取的U（提取后不更新会统计误差）
        //总量
        $machine = (new Machine())->where('status', 1)->select();
        $data['machines'] = [];
        foreach ($machine as $machines) {
            $data['machines'][] = [
                'name' => $machines['name'],
                'allcount' => (new MachineLog())->where('machineid', $machines['id'])->count(),
                'ingscount' => (new MachineLog())->where('machineid', $machines['id'])->where('status', 1)->count(),
                'allincome' => (new MachineLog())->where('machineid', $machines['id'])->sum('allincome'),
                'income' => (new MachineLog())->where('machineid', $machines['id'])->sum('income'),
                'todayincome' => (new MachineLog())->where('machineid', $machines['id'])->where('days', '>=', date('Y-m-d'))->sum('todayincome'),

            ];
        }

        return view(null, [
            'admin' => $this->request->admin,
            'data' => $data

        ]);
    }

    public function index()
    {
        $admin = $this->request->admin;
        $menu = config('menu.menu');
        $arr = [];
        foreach ($menu as $k => $v) {
            if ($v['show'] == true) {
                foreach ($v['sub'] as $x => $y) {
                    if (($y['show'] == true && in_array($y['uri'], $admin['role'])) || $admin['is_default'] == 1) {
                        $arr[$k]['name'] = $v['name'];
                        $arr[$k]['ico'] = $v['ico'];
                        $arr[$k]['sub'][$x] = $y;
                    }
                }
            }
        }
        return view(null, [
            'admin' => $admin,
            'menu' => $arr,
        ]);
    }


    public function miss()
    {
        return view('404');
    }

    public function admin()
    {
        $admin = Admin::get(session('admin_id'));
        if ($this->request->isPost()) {
            if (empty($admin)) {
                json(['status' => 1, 'msg' => '登陆已过期!']);
            }

            $data = input('post.');
            if ($data['password'] != '') {
                if ($data['password'] != $data['passworder']) {
                    return json(['status' => 1, 'info' => '密码不一致']);
                }
                $data1['password'] = md5(md5($data['password']) . md5($admin['entry']));
            }
            $data1['nickname'] = $data['nickname'];
            if ($admin->save($data1, ['id' => $admin->id])) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            return view('admin', ['admin' => $admin]);
        }
    }
}
